#!/usr/bin/env python

from math import log
import yaml


class TfIdf:
    '''
    Class to handle all tf-idf calculations.
    Maintains dictionary of stop-words as well as a tf-idf score
    dictionary

    This class does *NOT* implement analysis of documents based
    on tf-idf scores.
    '''

    def __init__(self):
        pass

    def profile_document(self, raw_document, nscores=100):
        '''
        Obtain tf-idf scores for nscores most common words, exculding stop words.
        Raw_document is a parsed and sanitized pdf file.
        '''
        pass
        # count words
        # exclude stop words
        # score
        # return list of [word, score] sublists

    def tf_idf(self, term, document):
        '''
        Calculates the tf-idf score for a given term string.
        Document is a dictionary containing {term -> count} pairs

        Returns 0.0 if term is a stop word
        '''
        # Santize term string
        term = self.sanitize(term)

        # Halt if stop word
        if term in self.stopwords:
            return 0.

        # Calculate term frequency (i.e.:  in-document)
        total_words = 0
        for key in document.keys():
            total_words += document[key]

        tf = float(document[term]) / total_words  # tf must be decimal

        # calculate idf
        idf = log(self.ndocs / float(self.corpus[term]), 10)  # log base 10

        return tf * idf

    def sanitize(self, string):
        return string.strip().lower()
